﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Xml.Linq;
using System.Collections.Generic;

using YouHP.Model;
using YouHP.BLL;

namespace web.admin
{
    public partial class PrescriptionList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            // Get all the diseases info.
            List<DiseaseInfo> diseaseList = DiseaseBLL.Instance.GetAllDisease();
            this.DiseaseNameList.DataSource = diseaseList;
            this.DiseaseNameList.DataTextField = "Name";
            this.DiseaseNameList.DataValueField = "Id";
            this.DiseaseNameList.DataBind();
        }

        private void DisableCheckBoxForClassicPrescription()
        {
            for (int rowIndex = 0; rowIndex < this.prescriptionListGridView.Rows.Count; rowIndex++)
            {
                bool ischeck = ((CheckBox)this.prescriptionListGridView.Rows[rowIndex].Cells[0].FindControl("chk")).Checked;
                if (ischeck)
                {
                    //((CheckBox)this.prescriptionListGridView.Rows[rowIndex].Cells[0].FindControl("chk")).Visible = false;
                    //((Label)this.prescriptionListGridView.Rows[rowIndex].Cells[0].FindControl("lblClassic")).Visible = true;
                }
            }
        }

        protected void btnGetPrescriptionList_Click(object sender, EventArgs e)
        {
            GetPrescriptionListFromDropDownListSelection();
        }

        protected void btnSetSelectedAsClassic_Click(object sender, EventArgs e)
        {
            try
            {
                int[] idList = new int[this.prescriptionListGridView.Rows.Count];
                bool[] checkList = new bool[this.prescriptionListGridView.Rows.Count];

                for (int rowIndex = 0; rowIndex < this.prescriptionListGridView.Rows.Count; rowIndex++)
                {
                    CheckBox cb = ((CheckBox)this.prescriptionListGridView.Rows[rowIndex].Cells[0].FindControl("chk"));
                    bool ischeck = cb.Checked;
                    bool isVisible = cb.Visible;
                    idList[rowIndex] = Convert.ToInt32(this.prescriptionListGridView.DataKeys[rowIndex].Value);
                    checkList[rowIndex] = ischeck;
                }

                PrescriptionBLL.Instance.UpdatePrescriptionsClassicState(idList, checkList);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            GetPrescriptionListFromDropDownListSelection();
            DisableCheckBoxForClassicPrescription();
        }

        private void GetPrescriptionListFromDropDownListSelection()
        {
            int diseaseId = -1;
            if (!Int32.TryParse(this.DiseaseNameList.SelectedValue, out diseaseId))
                return;

            string condStr = string.Format("[Prescription].[DiseaseId]={0} AND [Prescription].[ParentId] = 0", diseaseId);
            List<PrescriptionInfo> prescriptionList = PrescriptionBLL.Instance.GetPrescriptionList(condStr);
            this.prescriptionListGridView.DataSource = prescriptionList;
            this.prescriptionListGridView.DataBind();

            DisableCheckBoxForClassicPrescription();
        }

        protected void btnUpdateAllDoctorInfo_Click(object sender, EventArgs e)
        {
            if (DoctorBLL.Instance.UpdaetAllDoctorPrescritionAverageScoreAndCount())
            {
                Response.Write("<script language='javascript'>alert('成功!');</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>alert('失败!');</script>");
            }
        }

        protected void btnUpdateAllPrescriptionInfo_Click(object sender, EventArgs e)
        {
            if (PrescriptionBLL.Instance.UpdateAverageScoreOfAllPrescription())
            {
                Response.Write("<script language='javascript'>alert('成功!');</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>alert('失败!');</script>");
            }
        }
    }
}
